package lambdademo;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: MENG
 * Date: 2022-08-15
 * Time: 19:29
 */

/*
    @FunctionalInterface
    interface A{
        void test1();
        void test2();
        void test3();

    }*/

//无返回值无参数
@FunctionalInterface
interface NoParameterNoReturn {
    void test();
}
//无返回值一个参数
@FunctionalInterface
interface OneParameterNoReturn {
    void test(int a);
}

//无返回值多个参数
@FunctionalInterface
interface MoreParameterNoReturn {
    void test(int a,int b);
}

//有返回值无参数
@FunctionalInterface
interface NoParameterReturn {
    int test();
}

//有返回值一个参数
@FunctionalInterface
interface OneParameterReturn {
    int test(int a);
}
//有返回值多参数
@FunctionalInterface
interface MoreParameterReturn {
    int test(int a,int b);
}

class Demo{
    void func(){
        System.out.println("fafafafa");
    }

}
public class Test {
    //    PriorityQueue<Integer> priorityQueue = new PriorityQueue<>((o1, o2) -> o1-o2);
    public static void main(String[] args) {
        HashMap<Integer,String> hashMap = new HashMap<>();
        hashMap.put(1,"a");
        hashMap.put(2,"b");
        hashMap.put(3,"c");
        hashMap.put(4,"d");

        //普通方法实现hashMap的foreach方法
        hashMap.forEach(new BiConsumer<Integer, String>() {
            @Override
            public void accept(Integer integer, String s) {
                System.out.println("key:"+ integer + " val:"+s);
            }
        });

        //lambda方法实现hashMap的foreach方法
        hashMap.forEach((integer, s) -> System.out.println("key:"+ integer + " val:"+s));

    }
    public static void main8(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("b");
        list.add("a");
        list.add("c");
        list.add("d");

/*        //普通方法实现foreach打印
        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });*/

/*        //lambda实现foreach打印
        list.forEach(s-> System.out.println(s));*/

/*
        //普通方法实现sort
        list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        list.forEach(s-> System.out.println(s));
*/

/*        //Lambda表达式的方法实现sort
        list.sort((o1,o2)->o1.compareTo(o2));
        list.forEach(s-> System.out.println(s));*/

    }
    public static void main7(String[] args) {
/*        int a = 10;
        new Demo(){
            @Override
            void func() {
//                a = 99;
                int c = 10;
                c = 99;
                System.out.println("重写func");
                System.out.println(c);
            }
        }.func();*/

/*        int a = 10;
        MoreParameterNoReturn moreParameterNoReturn = (x,y)->{
            System.out.println(x+y);
            a = 99;
            System.out.println(a);
        };*/
    }
    public static void main6(String[] args) {
        MoreParameterReturn moreParameterReturn = (x,y)-> x+y;
        int ret = moreParameterReturn.test(15,20);
        System.out.println(ret);
    }
    public static void main5(String[] args) {
        OneParameterReturn oneParameterReturn = x->2*x;
        int ret = oneParameterReturn.test(15);
        System.out.println(ret);

        OneParameterReturn oneParameterReturn1 = x->20;
        int ret1 = oneParameterReturn1.test(5);
        System.out.println(ret1);
    }
    public static void main4(String[] args) {
        NoParameterReturn noParameterReturn = ()->{return 10;};
        int ret = noParameterReturn.test();
        System.out.println(ret);

        NoParameterReturn noParameterReturn1 = ()->100;
        int ret1 =noParameterReturn1.test();
        System.out.println(ret1);

    }

    public static void main3(String[] args) {
        MoreParameterNoReturn moreParameterNoReturn = (int a, int b)->{
            System.out.println(a+b);
        };
        moreParameterNoReturn.test(10,20);

        MoreParameterNoReturn moreParameterNoReturn1 = (a,b)-> System.out.println(a*b);
        moreParameterNoReturn1.test(10,20);
    }
    public static void main2(String[] args) {
        OneParameterNoReturn oneParameterNoReturn = (int x)->{
            System.out.println(x);
        };
        oneParameterNoReturn.test(100);

        OneParameterNoReturn oneParameterNoReturn1 = x-> System.out.println(x);
        oneParameterNoReturn1.test(200);

        OneParameterNoReturn oneParameterNoReturn2 = System.out::println;
        oneParameterNoReturn2.test(300);
    }

    public static void main1(String[] args) {
        NoParameterNoReturn noParameterNoReturn = new NoParameterNoReturn() {
            @Override
            public void test() {
                System.out.println("测试一下");
            }
        };
        noParameterNoReturn.test();

        NoParameterNoReturn noParameterNoReturn1 = ()->{System.out.println("测试一下1");};
        noParameterNoReturn1.test();

        NoParameterNoReturn noParameterNoReturn2 = ()-> System.out.println("测试一下2");
        noParameterNoReturn2.test();

        NoParameterNoReturn noParameterNoReturn3 = ()->{
            System.out.println("测试一下3");
            System.out.println("测试一下4");
        };
        noParameterNoReturn3.test();


    }


}
